package com.icss.mqsp.extend.login;

import com.icss.mqsp.builder.IBuilder;
import com.icss.mqsp.db.DBManager;
import com.icss.mqsp.db.R1DBDataPrivoder;
import com.icss.mqsp.util.DataUtil;
import com.icss.mqsp.util.RequestCondition;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoginBuilderSZ
  implements IBuilder
{
  private static Log log = LogFactory.getLog(LoginBuilderSZ.class);
  
  public JSONObject exec(RequestCondition rc)
  {
    String op = (String)rc.getCondition("opid");
    String username = (String)rc.getCondition("username");
    String password = (String)rc.getCondition("password");
    String modeltype = (String)rc.getCondition("modeltype");
    String name = "";
    String firm = "";
    String dpt = "";
    String mdpass = DataUtil.setPwdJM(password);
    String phoneuuid = (String)rc.getCondition("uuid");
    String model = (String)rc.getCondition("model");
    log.info("uuid值=" + phoneuuid + "model值=" + model);
    JSONObject json = new JSONObject();
    Map conditionMap = new HashMap();
    conditionMap.put("username", username);
    conditionMap.put("password", mdpass);
    conditionMap.put("phoneuuid", phoneuuid);
    conditionMap.put("model", model);
    conditionMap.put("modeltype", modeltype);
    if (op.equals("verson"))
    {
      log.info("获取版本信息");
      List<Map<String, String>> userList = DBManager.queryForList("getVersion", conditionMap);
      if (userList.size() > 0)
      {
        Map map = (Map)userList.get(0);
        Integer version = (Integer)map.get("VERSON");
        log.info(modeltype + "版本号：" + version);
        json.put("version", version);
      }
      else
      {
        log.info(modeltype + "版本信息不存在");
        json.put("version", "0");
      }
    }
    else if (op.equals("registe"))
    {
      log.info("用户注册");
      R1DBDataPrivoder R1DB = new R1DBDataPrivoder();
      List<Map<String, String>> rsList = R1DB.query("registeUser", conditionMap);
      if (rsList.size() > 0)
      {
        Map map = (Map)rsList.get(0);
        dpt = (String)map.get("CNNAME");
        name = (String)map.get("FULL_NAME");
        log.info("用户信息" + name);
        String puuid = (String)map.get("PARENTORGUUID");
        Integer orglevel = (Integer)map.get("ORGLEVEL");
        log.info("orglevel信息" + orglevel);
        log.info("dpt信息" + dpt);
        if (orglevel.intValue() != 2)
        {
          conditionMap.put("PUUID", puuid);
          List<Map<String, String>> orgList = R1DB.query("registeGetOrg", conditionMap);
          Map map1 = (Map)orgList.get(0);
          String orgname1 = (String)map1.get("CNNAME");
          String puuid1 = (String)map1.get("PARENTORGUUID");
          Integer orglevel1 = (Integer)map1.get("ORGLEVEL");
          log.info("orglevel1信息" + orglevel1);
          log.info("orgname1信息" + orgname1);
          if (orglevel1.intValue() != 2)
          {
            conditionMap.put("PUUID", puuid1);
            List<Map<String, String>> orgList2 = R1DB.query("registeGetOrg", conditionMap);
            Map map2 = (Map)orgList2.get(0);
            String orgname2 = (String)map2.get("CNNAME");
            log.info("orgname2信息" + orgname2);
            firm = orgname2;
            dpt = orgname1 + "-" + dpt;
          }
          else
          {
            firm = orgname1;
          }
        }
        else
        {
          firm = dpt;
        }
        log.info("用户公司" + firm + "-" + dpt);
        Map<String, String> insertUserMap = new HashMap();
        insertUserMap.put("USERNAME", username);
        insertUserMap.put("PASSWORD", mdpass);
        insertUserMap.put("MODEL", model);
        insertUserMap.put("UUID", phoneuuid);
        insertUserMap.put("FIRM", firm);
        insertUserMap.put("DEPARTMENT", dpt);
        insertUserMap.put("NAME", name);
        int ret = DBManager.insert("insertMqspUser", insertUserMap);
        if (ret == 1) {
          json.put("isValidUser", "3");
        } else {
          json.put("isValidUser", "2");
        }
      }
      else
      {
        json.put("isValidUser", "0");
      }
    }
    else if (op.equals("validate"))
    {
      log.info("启用停用");
      int flag = DBManager.update("validateUser", conditionMap);
      log.info("启用停用状态=" + flag);
      if (flag == 1) {
        json.put("validateUser", "1");
      } else {
        json.put("validateUser", "0");
      }
    }
    else if (op.equals("userManager"))
    {
      log.info("获取注册用户信息");
      conditionMap.put("time", DataUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
      List<Map<String, String>> dataList = DBManager.queryForList("getUserManage", conditionMap);
      String dataJson = DataUtil.listToJson(dataList);
      json.put("value", dataJson);
    }
    else
    {
      log.info("用户登录");
      List<Map<String, String>> userList = DBManager.queryForList("checkLogin", conditionMap);
      if (userList.size() > 0)
      {
        Map map = (Map)userList.get(0);
        String userFlag = (String)map.get("FLAG");
        if (userFlag.equals("1"))
        {
          log.info("用户验证通过");
          String usern = (String)map.get("USERNAME");
          log.info("用户id" + usern);
          json.put("isValidUser", "1");
          json.put("username", usern);
        }
        else
        {
          log.info("用户验证通过");
          json.put("isValidUser", "4");
        }
      }
      else
      {
        log.info("用户验证未通过");
        json.put("isValidUser", "5");
      }
    }
    return json;
  }
}
